package cn.stylefeng.guns.sms.modular.controller;

import cn.stylefeng.guns.sms.core.enums.SmsVerifyResult;
import cn.stylefeng.guns.sms.core.sms.SmsManager;
import cn.stylefeng.guns.sms.modular.model.SendMessageParam;
import cn.stylefeng.guns.sms.modular.model.VerifySMSParam;
import cn.stylefeng.guns.sms.modular.provider.SmsServiceApi;
import cn.stylefeng.guns.sms.modular.service.SmsInfoService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author : xiaofei
 * @Date: 2019/6/19
 */
@RequestMapping("/sms")
@RestController
public class SmsController extends BaseController {


    Logger logger = LoggerFactory.getLogger(SmsController.class);

    @Autowired
    private SmsServiceApi smsServiceApi;
    @Autowired
    private SmsInfoService smsInfoService;

    /**
     * 发送短信
     * @param sendMessageParam
     * @return
     */
    @RequestMapping("/send")
    public ResponseData sendSms(SendMessageParam sendMessageParam){


        logger.info("---------开始发送短信-----------");
        boolean b  = smsServiceApi.sendShortMessage(sendMessageParam);
        if (b){
            logger.info("----------发送短信成功------------");
            return  ResponseData.success(200,"发送短信成功",sendMessageParam);
        }else {
            logger.error("发送短信失败");
            return ResponseData.error("失败");
        }

    }


    @RequestMapping("/validate")
    public ResponseData validateSms(VerifySMSParam verifySMSParam){

       logger.info("--------开始验证短信验证码-----------");

     SmsVerifyResult verifyResult =  smsInfoService.validateSmsInfo(verifySMSParam);

     if ("验证成功".equals(verifyResult.getDesc())){
         return  SUCCESS_TIP;
     }else {
         return  ResponseData.error("验证失败");
     }
    }
}
